/*
 * $Id: DottedFunctionLayer.java,v 1.2 2003/06/01 11:19:02 znamenacek Exp $
 *
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.canvas;

import net.sf.vofce.project.Project;
import net.sf.vofce.project.Config;
import net.sf.vofce.guts.Function;

import java.awt.Graphics2D;
import java.awt.Color;

/**
 * Layer with function graphs.
 * 
 * @author Tomas Znamenacek
 */
public class DottedFunctionLayer implements Layer {

	/** Project instance. */
	protected Project project = Project.getInstance();

	/** Config instance. */
	protected Config config = Config.getInstance();
	
	/** Axis x. */
	protected Axis x;

	/** Axis y. */
	protected Axis y;

	/** Called by viewport when axis change. */
	public void setAxis(Axis x, Axis y) {

		this.x = x;
		this.y = y;
	}

	/** Called by viewport when it's time to render. */
	public void render(Graphics2D buffer) {

		for (int i = 0; i < project.getSize(); i++) {
			drawFunction(project.getFunction(i), config.getFunctionColor(i), buffer);
		}
	}
	
	/**
	 * Draws function on surface.
	 */
	protected void drawFunction(Function f, Color c, Graphics2D surface) {

		surface.setColor(c);
		double[] values = x.getValues();

		for (int i = 0; i < values.length; i++) {
	
			double value = f.evaluate(values[i]);

			if (! Double.isNaN(value)) {
				int coord = y.logicalToPhysical(value);
				if (coord != -1) {
					surface.drawLine(i, coord, i, coord);
				}
			}
		}
	}
}
